package com.pengpeng.android.dao;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.pengpeng.android.entity.AppEntity;
import com.pengpeng.android.entity.MapEntity;
import com.pengpeng.android.entity.ServerEntity;

public class ServerDao extends SuperDao{
	
	
	private void createTable()
	{
		String sqlstr="create table servers(";
		sqlstr+="servername varchar(20) not null,";
		sqlstr+="serverurl varchar(100) null";
		sqlstr+="ifavailable varchar(2) null,";
	    sqlstr+=" constraint PK_SERVERS primary key clustered (servername)";
	    sqlstr+=");";
		sqlitedb.execSQL(sqlstr);
	}//createTable
	
    public void insert(ServerEntity serverentity)
    {
    	String ifavailable;
    	if(true==serverentity.getIfavailable())
    	{
    		ifavailable="1";
    	}
    	else
    	{
    		ifavailable="0";
    	}
    	String sqlstr="insert into servers(";
    	sqlstr+="servername,serverurl,ifavailable ";
    	sqlstr+="values('"+serverentity.getServername()+"','";
    	sqlstr+=serverentity.getServerurl()+"','";
    	sqlstr+=ifavailable+"')";
		sqlitedb.execSQL(sqlstr);
    }//insert
    
    private void delete()
    {
    	//app的记录不能删除
    }//delete
    
    public void update(ServerEntity serverentity)
    {
    	String ifavailable;
    	if(true==serverentity.getIfavailable())
    	{
    		ifavailable="1";
    	}
    	else
    	{
    		ifavailable="0";
    	}
    	String sqlstr="update servers set ";
    	sqlstr+="servername='"+serverentity.getServername()+"',";
    	sqlstr+="serverurl='"+serverentity.getServerurl()+"',";
    	sqlstr+="ifavailable='"+ifavailable+"',";
		sqlitedb.execSQL(sqlstr);

    	
    }//update
    
    public ServerEntity queryByKey(String servername)
    {
		String serverurl;
		String ifavailablestr;
		boolean ifavailable;
		
    	String sqlstr="";
		Cursor cursor=null;
		int result=0;
		ServerEntity serverentity=null;
		sqlstr="select servername,serverurl,ifavailable ";
		sqlstr+="from servers where servername='"+servername+"'";
		cursor=sqlitedb.rawQuery(sqlstr, null);
		cursor.moveToFirst();
		if(!cursor.isAfterLast())
		{
			servername=cursor.getString(0);
			serverurl=cursor.getString(1);
			ifavailablestr=cursor.getString(2);
			if(ifavailablestr.equals("1"))
			{
				ifavailable=true;
			}
			else
			{
				ifavailable=false;
			}
	
			
			serverentity=new ServerEntity();
			serverentity.setServername(servername);
			serverentity.setServerurl(serverurl);
			serverentity.setIfavailable(ifavailable);
			
						
		}
		cursor.close();
        return serverentity;
    }//queryByKey
    
    public ArrayList<ServerEntity> query()
    {
    	String servername;
		String serverurl;
		String ifavailablestr;
		boolean ifavailable;
		
    	String sqlstr="";
		Cursor cursor=null;
		int result=0;
		ArrayList<ServerEntity> serverlist=new ArrayList<ServerEntity>();
		ServerEntity serverentity=null;
		sqlstr="select servername,serverurl,ifavailable ";
		sqlstr+="from servers";
		cursor=sqlitedb.rawQuery(sqlstr, null);
		cursor.moveToFirst();
		while(!cursor.isAfterLast())
		{
			servername=cursor.getString(0);
			serverurl=cursor.getString(1);
			ifavailablestr=cursor.getString(2);
			if(ifavailablestr.equals("1"))
			{
				ifavailable=true;
			}
			else
			{
				ifavailable=false;
			}
	
			
			serverentity=new ServerEntity();
			serverentity.setServername(servername);
			serverentity.setServerurl(serverurl);
			serverentity.setIfavailable(ifavailable);
			
			serverlist.add(serverentity);
						
		}
		cursor.close();
        return serverlist;
    }//query
    
    public void close()
    {
    	closeDatabase();
    }//close
    
    public ServerDao()
    {
    	openDatabase();
    	if(!existTable("servers"))
    	{
    		createTable();
    		ServerEntity serverentity=new ServerEntity();
    		serverentity.setServername("server1");
    		serverentity.setServerurl("http://www.gisformobile.com");
    		serverentity.setIfavailable(true);
    		insert(serverentity);
    	}
    }//AppDao
  
}
